
#include "CharBuffer.h"

using namespace ttg_internal;

//------------------
//--- CharBuffer ---
//------------------

CharBuffer::CharBuffer(int size)
{
	base = new char[size];
    maxsize = size;
    cursize = 0;
}

void CharBuffer::addChar(char c)
{
	if (cursize >= maxsize-1)
    {
        base = (char *)realloc(base, maxsize += INC_SIZE);
    }
    base[cursize++] = c;
}

void CharBuffer::addString(const char *s)
{
	size_t i, len;
    if (s == NULL)
        return;
    len = strlen(s);
	if (cursize + len >= maxsize - 1)
		base = (char *)realloc(base, maxsize += INC_SIZE > len + 1
														? INC_SIZE
														: len + 1);
    for (i = 0; i<len; i++)
        base[cursize++] = s[i];
}

char CharBuffer::removeChar()
{
	if (cursize > 0)
    {
        return base[cursize--];
    }
    else
        return 0;
}

const char *CharBuffer::showContent() const
{
	base[cursize] = '\0';
    return base;
}

void CharBuffer::clear()
{
	cursize = 0;
}

int CharBuffer::isEmpty()
{
	return cursize == 0;
}

CharBuffer::~CharBuffer()
{
	delete[] base;
}
